package com.yami.shop.dao;

import com.yami.shop.bean.app.dto.PostRecordDto;
import com.yami.shop.bean.app.dto.UserLikeRecordDto;
import com.yami.shop.bean.model.UserLikeRecord;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户喜欢记录 Mapper 接口
 * </p>
 *
 * @author chajiu
 * @since 2021-06-29
 */
public interface UserLikeRecordMapper extends BaseMapper<UserLikeRecord> {

    @Results(
            value = {
                    @Result(column = "from_user_id",property = "fromUserId",javaType =String.class ),
                    @Result(column = "like_user_id",property = "likeUserId",javaType =String.class ),
                    @Result(column = "create_time",property = "createTime",javaType = Date.class ),
                    @Result(column = "like_user_id",property = "likeUser",one = @One(select = "com.yami.shop.dao.UserMapper.getUserById",fetchType = FetchType.EAGER))
            }
    )
    @Select("select * from ch_user_like_record where from_user_id = #{uid} order by create_time desc")
    List<UserLikeRecordDto> getLikeListByUserId(@Param("uid") String uid);


    @Select("select count(*) from ch_user_like_record where from_user_id = #{uid}")
    Integer countMyLike(@Param("uid") String uid);

    @Select("select count(*) from ch_user_like_record where like_user_id = #{uid}")
    Integer countLikeMe(@Param("uid") String uid);


    /*@Results(
            value = {
                    @Result(column = "pic",property = "user.pic" ),
                    @Result(column = "sex",property = "user.sex" ),
                    @Result(column = "age",property = "user.age" ),
                    @Result(column = "nick_name",property = "user.nickName" ),
                    @Result(column = "id",property = "id" ),
                    @Result(column = "like_num",property = "likeNum" ),
                    @Result(column = "create_time",property = "createTime" ),
                    @Result(column = "comment",property = "comment" ),
                    @Result(column = "operator",property = "operator" )
            }
    )*/
    //查询50km内的用户动态
    @Select("SELECT us.nick_name,us.pic,us.sex,us.age,po.*, ( 6378.137 * acos( cos( radians(#{lat}) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(#{lng}) ) + sin( radians(#{lat}) ) * sin( radians( lat ) ) ) ) AS distance FROM tz_user us INNER JOIN ch_post_record po on us.user_id = po.user_id where po.`status` = 1 and po.is_del = 0 HAVING status = 1 and distance < 50 ORDER BY create_time desc")
    List<PostRecordDto> queryNeaMoveUser(@Param("lng") Double lng,@Param("lat") Double lat);

    //查询50km内的用户动态(Sex)
    @Select("SELECT us.nick_name,us.pic,us.sex,us.age,po.*, ( 6378.137 * acos( cos( radians(#{lat}) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(#{lng}) ) + sin( radians(#{lat}) ) * sin( radians( lat ) ) ) ) AS distance FROM tz_user us INNER JOIN ch_post_record po on us.user_id = po.user_id where po.`status` = 1 and po.is_del = 0 HAVING status = 1 and sex = 'F' and distance < 50 ORDER BY create_time desc")
    List<PostRecordDto> queryNeaMoveUserSex(@Param("lng") Double lng,@Param("lat") Double lat);

    //根据省分
    @Select("select us.nick_name,us.pic,us.sex,us.age,po.* from tz_user us inner join ch_post_record po on us.user_id = po.user_id where province = #{prov} and po.status = 1")
    List<PostRecordDto> queryRegion(@Param("prov") String prov);

    //根据省分(Sex)
    @Select("select us.nick_name,us.pic,us.sex,us.age,po.* from tz_user us inner join ch_post_record po on us.user_id = po.user_id where province = #{prov} and us.sex = 'F' and po.status = 1")
    List<PostRecordDto> queryRegionSex(@Param("prov") String prov);

}
